<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 23/9/14
 * Time: 12:56 PM
 */
use Laravel\Input;

class Admin_Settings_General_Controller extends Admin_Controller {

	public $restful = true;

	public function __construct()
	{
		parent::__construct();
		//--Your Code --//
		$this->theme->set_theme("admin") ;
		$this->theme->set_layout("default") ;


	}

	public function get_index(){
		if (Auth::guest()) return Redirect::to('admin/login');

		if (!Auth::check())
		{
			return Redirect::to('admin/login');
		}else{
			$data['lang'] = $this->lang;
			$data['title'] = Settings::get($this->lang, 'site_title');
			$this->theme->title("Application Settings");

			return $this->theme->render('admin::settings.general',$data);
		}
	}

	public function post_index(){


		if(Input::get('btnSave') == 'notification'){

			if(Input::get('not_admin_reg')){
				Settings::saveSetting('notify_admin_registration', trim(Input::get('not_admin_reg')));
			}else{
				Settings::saveSetting('notify_admin_registration', 'off');
			}

			if(Input::get('not_admin_post')){
				Settings::saveSetting('notify_admin_post', trim(Input::get('not_admin_post')));
			}else{
				Settings::saveSetting('notify_admin_post', 'off');
			}

			if(Input::get('not_admin_comment')){
				Settings::saveSetting('notify_admin_comment', trim(Input::get('not_admin_comment')));
			}else{
				Settings::saveSetting('notify_admin_comment', 'off');
			}

			if(Input::get('not_admin_incorrect')){
				Settings::saveSetting('notify_admin_incorrect', trim(Input::get('not_admin_incorrect')));
			}else{
				Settings::saveSetting('notify_admin_incorrect', 'off');
			}

			if(Input::get('not_user_registration')){
				Settings::saveSetting('notify_user_registration',trim(Input::get('not_user_registration')));
			}else{
				Settings::saveSetting('notify_user_registration', 'off');
			}

			if(Input::get('not_user_post')){
				Settings::saveSetting('notify_user_post',trim(Input::get('not_user_post')));
			}else{
				Settings::saveSetting('notify_user_post', 'off');
			}

			if(Input::get('mod_post')){
				Settings::saveSetting('mod_post',trim(Input::get('mod_post')));
			}else{
				Settings::saveSetting('mod_post', 'off');
			}

			if(Input::get('mod_comment')){
				Settings::saveSetting('mod_comment',trim(Input::get('mod_comment')));
			}else{
				Settings::saveSetting('mod_comment', 'off');
			}

			if(Input::get('mod_photo')){
				Settings::saveSetting('mod_photo',trim(Input::get('mod_photo')));
			}else{
				Settings::saveSetting('mod_photo', 'off');
			}

			if(Input::get('mod_video')){
				Settings::saveSetting('mod_video',trim(Input::get('mod_video')));
			}else{
				Settings::saveSetting('mod_video', 'off');
			}


		}


		if (Input::get('btnSave') == 'address') {

			Settings::saveSetting('office_company', trim(Input::get('company')));
			Settings::saveSetting('office_address', trim(Input::get('address')));
			Settings::saveSetting('office_contact_1', trim(Input::get('phone_mobile')));
			Settings::saveSetting('office_contact_2', trim(Input::get('phone_landline')));
			Settings::saveSetting('office_email', trim(Input::get('email')));
			return Redirect::back()->with_input()->with('message','Settings Updated...');
		}

		if (Input::get('btnSave') == 'save') {

			$filename = Input::file('logo_file.name');

			//Upload the file
			$image = Input::file('logo_file');
			// $layer = PHPImageWorkshop\ImageWorkshop::initFromPath($image['tmp_name']);

			$dirPath = path('storage').'uploads/images/thumbnails/';
			//$filename = Auth::user()->id. uniqid($image['size']) .'.jpg';
			$filename = $image['name'];

			$rules = array(
					 
					'site_name' => 'required',
					'site_slogan' => 'required',
					'default_location' => 'required',
					'meta_title' => 'required',
			);
			 
			$validator = Validator::make(Input::get(), $rules);
			if( $validator->fails() )
			{
				return Redirect::back()->with_input()->with_errors($validator)->with('message','<div class="alert alert-danger">Invalid Operation !</div>');

			}else{
				if($filename == TRUE){

					 
					 


					$createFolders = true;
					$backgroundColor = null;
					$imageQuality = 100;

					$layer = PHPImageWorkshop\ImageWorkshop::initFromPath($image['tmp_name']);

					$thumbWidth = 150; // px
					$thumbHeight = 45;
					$conserveProportion = true;
					$positionX = 0; // px
					$positionY = 0; // px
					$position = 'LT';

					$layer->resizeInPixel($thumbWidth, $thumbHeight, $conserveProportion, $positionX, $positionY, $position);
					$layer->save($dirPath, $filename, $createFolders, $backgroundColor, $imageQuality);

					Settings::saveSetting('site_logo', trim($filename));

					Settings::saveSetting('site_title',trim(Input::get('site_name')));

					Settings::saveSetting('site_slogan',trim(Input::get('site_slogan')));

					Settings::saveSetting('default_location',trim(Input::get('default_location')));

					Settings::saveSetting('site_mode',trim(Input::get('site_mode')));

					Settings::saveSetting('site_mode',trim(Input::get('site_mode')));
					
					Settings::saveSetting('meta_title',trim(Input::get('meta_title')));
						
					Settings::saveSetting('meta_keyword',trim(Input::get('meta_keyword')));
						
					Settings::saveSetting('meta_description',trim(Input::get('meta_description')));

					Settings::saveSetting('footer_text',trim(Input::get('footer_area')));
				}
				else{
					 
					Settings::saveSetting('site_title',trim(Input::get('site_name')));

					Settings::saveSetting('site_slogan',trim(Input::get('site_slogan')));

					Settings::saveSetting('default_location',trim(Input::get('default_location')));

					Settings::saveSetting('site_mode',trim(Input::get('site_mode')));

					Settings::saveSetting('site_mode',trim(Input::get('site_mode')));
					
					Settings::saveSetting('meta_title',trim(Input::get('meta_title')));
						
					Settings::saveSetting('meta_keyword',trim(Input::get('meta_keyword')));
						
					Settings::saveSetting('meta_description',trim(Input::get('meta_description')));

					Settings::saveSetting('google_analytics',trim(Input::get('google_analytics')));
					
					Settings::saveSetting('publisher_id',trim(Input::get('publisher_id')));
					
					Settings::saveSetting('footer_text',trim(Input::get('footer_area')));
				}
			}
			//die($filename);
			/* $createFolders = true;
			$backgroundColor = null; // transparent, only for PNG (otherwise it will be white if set null)
			$imageQuality = 100; // useless for GIF, usefull for PNG and JPEG (0 to 100%)
			$layer->resizeInPixel(377, 310, true, 0, 0, 'MM');
			$layer->save($dirPath, $filename, $createFolders, $backgroundColor, $imageQuality);*/


			/*if(Input::upload('logo_file',  'upload/', $filename)){
			 Settings::saveSetting('site_logo', trim($filename));
			}*/


		}
		
		if(Input::get('btnSave') == 'social'){
			
			Settings::saveSetting('social_facebook',trim(Input::get('social_facebook')));
			
			Settings::saveSetting('social_twitter',trim(Input::get('social_twitter')));
			
			Settings::saveSetting('social_google',trim(Input::get('social_google')));
			
			Settings::saveSetting('social_linkdin',trim(Input::get('social_linkdin')));
			
		}
		return Redirect::back()->with('message','<div class="alert alert-info">Settings Updated...</div>');
	}
	
	
	
}
